<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * save or update uploaded file information on behalf of a users device
 * 
 * @param args = {
 * 
 *  @param id
 *  @param name
 *  @param uri
 *  @param mime
 *  @param size
 *  @param access
 *  @param modified
 *  @param user_id
 *  @param device_id
 *  @param chunks
 *  @param chunk
 *  @param added_ts
 *  @param modified_ts
 *  @param status
 * 
 * }
 */
function files_save($args){
    
    //import required global variables 
    global $db;
    global $contex;
    
    //extract and sanitize parameters creating a variable for each
    $fields = X_param_array ($args, 'fields'); 
    $id = X_param ($args, 'id'); 
    $data['name'] = X_param ($args, 'name'); if(!$id && !$data['name']) return __MOOV_PARAM_INVAL_;
    $data['uri'] = X_param ($args, 'uri'); if(!$id && !$data['uri']) return __MOOV_PARAM_INVAL_; 
    $data['mime'] = X_param ($args, 'mime'); if(!$id && !$data['mime']) return __MOOV_PARAM_INVAL_; 
    $data['size'] = X_param ($args, 'size'); if(!$id && !$data['size']) return __MOOV_PARAM_INVAL_; 
    $data['access'] = X_param ($args, 'access'); 
    $data['user_id'] = X_param ($args, 'user_id'); if(!$id && !$data['user_id']) return __MOOV_PARAM_INVAL_;
    $data['device_id'] = X_param ($args, 'device_id'); if(!$id && !$data['device_id']) return __MOOV_PARAM_INVAL_; 
    $data['chunks'] = X_param ($args, 'chunks'); 
    $data['chunk'] = X_param ($args, 'chunk'); 
    $data['added_ts'] = X_param ($args, 'added_ts', gmdate('y-m-d h:m:s')); 
    $data['modified_ts'] = X_param ($args, 'modified_ts', gmdate('y-m-d h:m:s'));
    $data['status'] = X_param ($args, 'status', 'A'); 
    $limit = X_param_array ($args, 'limit');
    $sort = X_param_array ($args, 'sort');
    
    // ---------------- Operation begins here ---------------------
    $id = $db->save('devices',$id,$data);
    return files_list(array('id' => $id, 'fields' => $fields, 'limit' => $limit, 'sort' => $sort));    

}

/*
 * retrieve uploaded file information on behalf of a users device
 * 
 * @param args = {
 * 
 *  @param id
 *  @param name
 *  @param uri
 *  @param mime
 *  @param size
 *  @param access
 *  @param modified
 *  @param user_id
 *  @param device_id
 *  @param chunks
 *  @param chunk
 *  @param added_ts
 *  @param modified_ts
 *  @param status
 * 
 * }
 */
function files_list($args){
    
    //import required global variables 
    global $db;
    global $contex;

    //extract and sanitize parameters creating a variable for each
    $fields = X_param_array ($args, 'fields'); 
    $data['id'] = X_param ($args, 'id'); 
    $data['name'] = X_param ($args, 'name'); 
    $data['uri'] = X_param ($args, 'uri');  
    $data['mime'] = X_param ($args, 'mime');  
    $data['size'] = X_param ($args, 'size');  
    $data['access'] = X_param ($args, 'access'); 
    $data['user_id'] = X_param ($args, 'user_id'); 
    $data['device_id'] = X_param ($args, 'device_id');  
    $data['chunks'] = X_param ($args, 'chunks'); 
    $data['chunk'] = X_param ($args, 'chunk'); 
    $data['added_ts'] = X_param ($args, 'added_ts'); 
    $data['modified_ts'] = X_param ($args, 'modified_ts');
    $data['status'] = X_param ($args, 'status', 'A'); 
    $limit = X_param_array ($args, 'limit');
    $sort = X_param_array ($args, 'sort');
    // ---------------- Operation begins here ---------------------
    $rc = $db->get('devices', $fields, $data, $limit, $sort);
    return out($rc);
}
